package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;


@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {


    @Reference //远程查找
    private CheckGroupService checkGroupService;

    //新增检查组
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkgroup,Integer [] checkitemIds){
        try{
            checkGroupService.add(checkgroup,checkitemIds);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    //编辑方法
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkgroup,Integer [] checkitemIds){
        try{
            checkGroupService.edit(checkgroup,checkitemIds);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }


    //分页查询
    //检查项查询 分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        PageResult pageResult = checkGroupService.pageQuery(queryPageBean);
        return pageResult;
    }

    //按ID查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            CheckGroup checkgroup=checkGroupService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkgroup);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    //检查组中关联检查项id查询
    @RequestMapping("/findCheckItemIdByCheckGroupId")
    public Result findCheckItemIdByCheckGroupId(Integer id){


        try{
           List<Integer> checkitemIds=checkGroupService.findCheckItemIdByCheckGroupId(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkitemIds);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //删除检查组
    @RequestMapping("delete")
    public Result delete(Integer id){
        //得到id 根据id进行删除
        try{
            checkGroupService.delete(id);
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

}
